package com.yz.crm.company.service.impl;

import com.yz.common.security.bcrypt.BCrypt;
import com.yz.common.security.crypt.AESUtils;
import com.yz.common.util.IDUtils;
import com.yz.crm.company.service.CompanyService;
import com.yz.crm.db.dao.BaseCompanyMapper;
import com.yz.crm.db.model.BaseCompany;
import com.yz.crm.db.model.BaseCompanyExample;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

/**
 * Created by DOVE on 2017/5/15 0015.
 */
@Service("segisterService")
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private SqlSession sqlSession;

    private BaseCompanyMapper getMapper() {
        return sqlSession.getMapper(BaseCompanyMapper.class);
    }
    
    @Override
    public BaseCompany get(String id) {
    	return getMapper().selectByPrimaryKey(id);
    }

    @Override
    public synchronized boolean add(BaseCompany company) {

        company.setId(IDUtils.getUUID());
        company.setCreateDate(new Date());
        company.setModifyDate(company.getCreateDate());
        company.setVerifyStatus(1);// 认证状态未知

        /*String securedPassword = BCrypt.hashpw(company.getPassword(),
                BCrypt.gensalt(12));
        company.setPassword(securedPassword);*/

        BaseCompanyMapper mapper = this.getMapper();

        return mapper.insert(company) > 0;
    }

    @Override
    public BaseCompany getByUsername(String username, String password) {
        BaseCompany companyNew = getByUsername(username);
        if (companyNew == null) {
            return null;
        }
        boolean matched = BCrypt.checkpw(password, companyNew.getPassword());
        if (!matched) {
            return null;
        }
        return companyNew;
    }

    /**
     * 验证用户名是否已经存在
     *
     * @param username
     * @return
     * @throws Exception
     */
    public BaseCompany getByUsername(String username) {
        BaseCompanyExample example = new BaseCompanyExample();
        example.createCriteria().andUsernameEqualTo(username);
        BaseCompanyMapper mapper = this.getMapper();
        List<BaseCompany> list = mapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        BaseCompany companyNew = list.get(0);
        return companyNew;
    }

    @Override
    public BaseCompany getByName(String name) throws Exception {
        BaseCompanyExample example = new BaseCompanyExample();
        example.createCriteria().andNameEqualTo(name);
        BaseCompanyMapper mapper = this.getMapper();
        List<BaseCompany> list = mapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        BaseCompany companyNew = list.get(0);
        return companyNew;
    }

    @Override
    public BaseCompany getByMail(String mail) throws Exception {
        BaseCompanyExample example = new BaseCompanyExample();
        example.createCriteria().andMailEqualTo(mail);
        BaseCompanyMapper mapper = this.getMapper();
        List<BaseCompany> list = mapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        BaseCompany companyNew = list.get(0);
        return companyNew;
    }

    @Override
    public BaseCompany getPhoneNo(String phoneNo) throws Exception {
        BaseCompanyExample example = new BaseCompanyExample();
        example.createCriteria().andContactNumberEqualTo(phoneNo);
        BaseCompanyMapper mapper = this.getMapper();
        List<BaseCompany> list = mapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        BaseCompany companyNew = list.get(0);
        return companyNew;
    }
}
